package project.main;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import project.crawler.downloader.PageDownloader;
import project.dbaccesslayer.MySQLAccessLayer;
import project.dbaccesslayer.Table_Questions_To_Download;
import project.dbaccesslayer.Table_Questions_To_Ignore;
import project.dbaccesslayer.Table_SO_Questions_Raw_html;

public class DownloadQuestions {

	public static void main(String[] args) throws IOException, SQLException {
		if (args.length != 1) {
			System.out.println("Usage: DownloadQuestions <limit>");
			return;
		}
		int limit = Integer.parseInt(args[0]);

		Connection conn = MySQLAccessLayer.openConnectionToMySql();

		while (limit > 0) {
			List<Long> list = Table_Questions_To_Download
					.getQuestionsToDownload(conn);
			System.out
					.println("Got " + list.size() + " questions to download.");

			if (list.isEmpty()) {
				break;
			}

			for (long questionID : list) {
				try {
					String html = PageDownloader
							.downloadQuestionRawHtml(questionID);
					Table_SO_Questions_Raw_html.saveQuestionRawHtml(conn,
							questionID, html);
				} catch (Exception e) {
					System.out.println(e);
					Table_Questions_To_Ignore.saveQuestionId(conn, questionID,
							e.toString());
				}

			}

			limit -= list.size();
		}

		System.out.println("Done.");

		conn.close();
	}

}
